//
//  render_target.h
//  PixelDudes
//
//  Created by Noah Figg on 3/23/13.
//  Copyright (c) 2013 PixelDudes. All rights reserved.
//

#ifndef _RENDER_TARGET_H_
#define _RENDER_TARGET_H_

#include "SDL.h"
#include <string>
#include <vector>
#include <unordered_set>
#include "pd_types.h"
#include "id_component.h"
#include "timer.h"

enum {
	RENDER_TARGET_ADD_TOP = -100,
	RENDER_TARGET_ADD_BOTTOM = -99
};

void RenderCallback(void* target, Timer* timer);

/* Linked list that sorts by z order */
class RenderTarget {
public:
    
    virtual void Update(double timeDelta);
    virtual void Render(SDL_Surface* screen);
	virtual void ReachedDestination() {}
	virtual void TimerFinished(Timer* timer) {}
    RenderTarget(std::string id);
    virtual ~RenderTarget();
    
	virtual void getScreenCoords(Sint16 &x, Sint16 &y); //Get screen coordinates using childrenRelative and parent structure to calculate it
	double getZOrder();
    void setZOrder(double z);
    void setPosition(double x, double y);
    void setDestPosition(double x, double y);
	bool isAtDestination();
    void setRelativePosition(double x, double y);
	void getPosition(double &x, double &y);
	void getDestPosition(double &x, double &y);
	void getLastPosition(double &x, double &y);
	void getPositionRelativeToNode(RenderTarget* ancestorNode, double &x, double &y);
	void setVisible(bool isVisible);
	bool getVisible();
	void setRelativeToParent(bool relative);
	bool getRelativeToParent();
    RenderTarget* getNext();
	RenderTarget* getChildren();
	RenderTarget* getLast();
	RenderTarget* getParent();
    void addChild(RenderTarget* child);
    bool removeChild(RenderTarget* child);
    void removeFromParent();
    void removeAllChildren(bool deleteChildren);
    Uint16 size();
	std::vector<RenderTarget*>* findChildrenWithTag(std::string tag);
	RenderTarget* findChildWithId(std::string id);
	RenderTargetComponent* getComponentType(std::string typeName);
	IdComponent* getIdComp();
	void setMoveSpeed(double speed);
	double getMoveSpeed();
protected:
    PD_vect3 renderTo;
	PD_vect3 lastRenderTo;
	PD_vect3 destRenderTo;
	double moveSpeed;
    //double zOrder;
    bool relativeToParent, visible;
    RenderTarget* next;
    RenderTarget* renderChildren;
    RenderTarget* parent;
	//std::string id;
	//std::unordered_set<std::string> tags;
	std::vector<RenderTargetComponent*> components;

    void clearUpperLinks();
	void renumberZ(double start, double increment);
};


#endif
